package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.mapper.StudentMapper;
import com.itheima.pojo.Clazz;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Student;
import com.itheima.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentMapper studentMapper;


    //分页查询学生列表
    @Override
    public PageResult<Student> page(String name, Integer degree, Integer clazzId, Integer page, Integer pageSize) {

        //设置分页参数
        PageHelper.startPage(page, pageSize);
        //查询数据
        List<Student> clazzList = studentMapper.page(name, degree, clazzId);
        //强转为page对象
        Page<Student> pageResult = (Page<Student>) clazzList;
        return new PageResult<>(pageResult.getTotal(),pageResult);

    }

    //添加学员
    @Override
    public void add(Student student) {
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());
        student.setViolationCount((short) 0);
        student.setViolationScore((short) 0);
        studentMapper.add(student);
    }

    //根据id查找学员
    @Override
    public Student getInfo(Integer id) {
        return studentMapper.getInfo(id);
    }

    //修改学员
    @Override
    public void update(Student student) {
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.update(student);
    }

    //删除学员
    @Override
    public void delete(List<Integer> ids) {
        studentMapper.delete(ids);
    }

    //根据id修改学员的违规次数和违规分数
    @Override
    public void violation(Integer id, Integer score) {
        studentMapper.violation(id,score);
    }
}
